using System;
using Mitosoft.Definitions.General;

namespace Mitosoft.Definitions.Data {
  
  public static class DataFromTo {

    public static Double DataToDouble( Object dbValue ) {
      if (dbValue is DBNull) return Convert.ToDouble(0);
      return (Double)dbValue;
    }

    public static BoolState DataToBoolState( Object dbValue ) {
      if (dbValue is DBNull) return BoolState.Null;
      if (!(dbValue is bool)) return BoolState.Undefined;
      if ((bool)dbValue) return BoolState.Set;
      return BoolState.Unset;
    }

    public static String DataToString( Object dbValue ) {
      if (dbValue is DBNull) return null;
      return dbValue.ToString();
    }

    public static System.DateTime DataToDateTime( Object dbValue ) {
      if (dbValue is DBNull) return System.DateTime.MinValue;
      if (!(dbValue is System.DateTime)) return System.DateTime.MinValue;
      return (System.DateTime)dbValue;
    }

    public static long DataToLong( Object dbValue ) {
      if (dbValue is DBNull) return 0;
      if (dbValue is long) return (long)dbValue;
      if (dbValue is int) return (int)dbValue;
      return 0;
    }

    public static int DataToInt( Object dbValue ) {
      if (dbValue is DBNull) return 0;
      if (dbValue is short) return Convert.ToInt32(dbValue);
      if (!(dbValue is int)) return 0;
      return (int)dbValue;
    }

  }

}
